import Cookies from 'js-cookie';

const user = {
    state: {
        userInfo: null
    },
    getters: {
        userId: state => {
            if (state.userInfo) {
                return state.userInfo.id;
            }
            const userInfo = Cookies.get('userInfo');
            if (userInfo) {
                return JSON.parse(userInfo).id;
            }
            return null;
        }
    },
    mutations: {
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
        },
        logout(state) {
            Cookies.remove('userInfo');
            localStorage.clear();
            state.userInfo = null;
        }
    }
};

export default user;
